package com.medication.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.medication.model.LimitMedication;
import com.medication.model.ThreeMedicationDir;
import com.medication.service.LimitMedicationService;
import com.medication.service.ThreeMedicationDirService;
import com.medication.utils.StringUtils;

public class IndexController extends Controller {

	public void index() {
		render("index.html");
	}

	public void list() throws UnsupportedEncodingException {
		String code = getPara("code");
		String name = getPara("name");
		String enCode = "";
		String enName = "";
		if (!StringUtils.isEmpty(name)) {
			name = URLDecoder.decode(name, "UTF-8");
			enName= URLEncoder.encode(name, "UTF-8");
		}
		if (!StringUtils.isEmpty(code)) {
			code = URLDecoder.decode(code, "UTF-8");
			enCode = URLEncoder.encode(code, "UTF-8");
		}
		Integer pageNumber = getParaToInt("pageNumber", 1);
		Page<ThreeMedicationDir> page = ThreeMedicationDir.dao.query(pageNumber, code, name);
		setAttr("page", page);
		setAttr("name", name);
		setAttr("code", code);
		setAttr("urlParam", "&code" +enCode + "&name="+enName);
		render("index.html");
	}

	/**
	 * 导入限制类药品
	 */
	public void importLimit() {
		String msg = "";
		UploadFile uploadFile = getFile();
		LimitMedicationService service = new LimitMedicationService();
		try {
			msg = service.handler(uploadFile.getFile());
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		setAttr("msg", msg);
		render("index.html");
	}

	public void exportLimit() {
		LimitMedicationService service = new LimitMedicationService();
		List<LimitMedication> datas = LimitMedication.dao.find("select * from limit_medication order by code");
		File file = service.exportExcel(datas);
		if (file != null) {
			renderFile(file);
		} else {
			setAttr("msg", "导出失败，请联系管理员");
			redirect("/admin");
		}

	}

	/**
	 * 导入三大目录药品
	 */
	public void importThree() {
		String msg = "";
		UploadFile uploadFile = getFile();
		ThreeMedicationDirService service = new ThreeMedicationDirService();
		try {
			msg = service.handler(uploadFile.getFile());
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		setAttr("msg", msg);
		render("index.html");
	}

	public void exportThree() {
		ThreeMedicationDirService service = new ThreeMedicationDirService();
		List<ThreeMedicationDir> datas = ThreeMedicationDir.dao
				.find("select * from three_medication_dir order by code");
		File file = service.exportExcel(datas);
		if (file != null) {
			renderFile(file);
		} else {
			setAttr("msg", "导出失败，请联系管理员");
			redirect("index.html");
		}

	}

	public void edit() {
		String code = getPara("code");
		ThreeMedicationDir three = null;
		LimitMedication limit = null;
		if (!StringUtils.isEmpty(code)) {
			three = ThreeMedicationDir.dao.findFirst("select * from three_medication_dir where code =?", code);
			limit = LimitMedication.dao.findFirst("select * from limit_medication where code =?", code);
		}

		if (three == null) {
			three = new ThreeMedicationDir();
		}

		if (limit == null) {
			limit = new LimitMedication();
		}
		setAttr("three", three);
		setAttr("limit", limit);
		render("edit.html");
	}

	public void save() {
		ThreeMedicationDir three = getModel(ThreeMedicationDir.class, "three");
		LimitMedication limit = getModel(LimitMedication.class, "limit");
		if (three.getInt("id") != null) {
			three.update();
		} else {
			three.save();
		}

		if (limit.getInt("id") != null) {
			limit.update();
		} else {
			limit.save();
		}
		setAttr("msg", "保存成功");
		setAttr("three", three);
		setAttr("limit", limit);
		render("edit.html");

	}

}
